//
//  ByCoreGuideViewController.m
//  beibei_ios
//
//  Created by pro on 16/5/24.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByCoreGuideViewController.h"

NSString *const versionKey = @"version_key";

#pragma mark ----------缓存类-----------
/** 缓存版本类 */
@interface CoreArchive : NSObject

/** 缓存 字符串 */
+ (void)setStr:(NSString *)str key:(NSString *)key;

/** 读取 字符串 */
+ (NSString *)strForKey:(NSString *)key;

@end

@implementation CoreArchive
// 保存普通对象
+ (void)setStr:(NSString *)str key:(NSString *)key{
    
    // 获取preference
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    // 保存
    [defaults setObject:str forKey:key];
    
    // 立即同步
    [defaults synchronize];
    
}

// 读取
+ (NSString *)strForKey:(NSString *)key{
    
    //获取preference
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    //读取
    NSString *str=(NSString *)[defaults objectForKey:key];
    
    return str;
}
@end

#pragma mark ----------滑动视图-----------
@interface NewFeatureScrollView : UIScrollView

@end
@implementation NewFeatureScrollView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if(self = [super initWithFrame:frame]){
        [self viewPrepare];
    }
    
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
    
    if(self = [super initWithCoder:aDecoder]){
        [self viewPrepare];
    }
    
    return self;
}
- (void)viewPrepare{
    
    self.pagingEnabled = YES;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.bounces = NO;
}
/** 布局子视图 */
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    __block CGRect frame = self.bounds;
    __block NSUInteger count = 0;
    [self.subviews enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
        
        if([subView isKindOfClass:[UIImageView class]]){
            
            CGFloat frameX = frame.size.width * idx;
            frame.origin.x = frameX;
            subView.frame = frame;
            count ++;
        }
    }];
    self.contentSize = CGSizeMake(frame.size.width * count, 0);
}

@end

#pragma mark -------------引导页--------------
@interface ByCoreGuideViewController ()

/** 图片数组数组 */
@property (nonatomic,strong) NSArray *imageNames;

/** scrollView */
@property (nonatomic,weak) NewFeatureScrollView *scrollView;

/** 进入首页按钮 */
@property (nonatomic,weak) UIButton *enterButton;

@property (nonatomic,copy) void(^enterBlock)();

@end

@implementation ByCoreGuideViewController

// 是否应该展示引导页面
+ (BOOL)needShowGuide{
    
    // 获取当前的版本号
    NSString *versionCurrent = APP_VERSION;
    NSLog(@"当前版本:%@", versionCurrent);
    // 读取本地版本号
    NSString *versionLocal = [CoreArchive strForKey:versionKey];
    NSLog(@"本地版本:%@", versionLocal);

    // 存在本地版本记录，且和当前系统版本一致，则不需要展示引导页面
    if(versionLocal!=nil && [versionCurrent isEqualToString:versionLocal]) {
        return NO;
    } else { // 无本地版本记录或本地版本记录与当前系统版本不一致
        // 保存当前开启app的版本号
        [CoreArchive setStr:versionCurrent key:versionKey];
        
        return YES;
    }
}

// 初始化滚动图片新特性界面
+ (instancetype)newFeatureVCWithImageNames:(NSArray *)imageNames enterBlock:(void(^)())enterBlock configuration:(void (^)(UIButton *enterButton))configurationBlock {
    
    ByCoreGuideViewController *guideVC = [[ByCoreGuideViewController alloc] init];
    
    guideVC.imageNames = imageNames;
    
    // 图片滑动视图
    configurationBlock([guideVC imgScrollPrepare]);
    
    // 显示了版本新特性，保存版本号
    [guideVC saveVersion];
    
    // 记录block
    guideVC.enterBlock = enterBlock;
    
    return guideVC;
}

// 显示了版本新特性，保存版本号
- (void)saveVersion{
    
    // 系统直接读取的版本号
    NSString *versionCurrent = APP_VERSION;
    // 保存版本号
    [CoreArchive setStr:versionCurrent key:versionKey];
}


#pragma mark -- 初始化图片滑动视图 --
- (UIButton *)imgScrollPrepare{
    
    // 添加scrollView
    NewFeatureScrollView *scrollView = [[NewFeatureScrollView alloc] init];
    _scrollView = scrollView;
    [self.view addSubview:scrollView];
    scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    // 给scrollView添加图片
    return [self imageViewsPrepare];
}

// 给scrollView添加图片
- (UIButton *)imageViewsPrepare{
    
    [self.imageNames enumerateObjectsUsingBlock:^(NSString *imageName, NSUInteger idx, BOOL *stop) {
        
        UIImageView *imgView = [[UIImageView alloc] init];
        imgView.image = [UIImage imageNamed:imageName];
        imgView.tag = idx;
        
        [_scrollView addSubview:imgView];
        
        if(idx == self.imageNames.count - 1) { // 最后一张图片，添加一个按钮，用于切换成app首页
            _enterButton = [self setUpEnterButton:imgView];
        }
    }];
    
//    _enterButton = [self setUpEnterButtonInView:self.view];
    return _enterButton;
}

// 创建进入按钮按钮
- (UIButton *)setUpEnterButtonInView:(UIView *)view {
    UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [view addSubview:enterButton];
    [enterButton addTarget:self action:@selector(chickEnterButton) forControlEvents:UIControlEventTouchUpInside];
    return enterButton;
}
// 创建进入按钮按钮
- (UIButton *)setUpEnterButton:(UIImageView *)imageView {
    imageView.userInteractionEnabled = YES;
    UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [imageView addSubview:enterButton];
    [enterButton addTarget:self action:@selector(chickEnterButton) forControlEvents:UIControlEventTouchUpInside];
    return enterButton;
}
// 按钮点击事件
- (void)chickEnterButton {
    [self dismiss];
}

#pragma mark -- 完成 --
- (void)dismiss {
    NSAssert(self.enterBlock, @"切换控制器的block不能为空!");
    
    _enterBlock();
}


@end
